package com.hdax.dm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.dto.ItemCinemaDto;
import com.hdax.dm.dto.ItemDto;
import com.hdax.dm.entity.item.DmItem;
import com.hdax.dm.mappers.MultipleMapper;
import com.hdax.dm.mapstrucs.ItemCinemaDtoMapper;
import com.hdax.dm.mapstrucs.ItemDtoMapper;
import com.hdax.dm.service.MultipleService;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

@Service
public class MultipleSImpl extends ServiceImpl<MultipleMapper, DmItem> implements MultipleService {

    @Autowired
    private ItemCinemaDtoMapper itemCinemaDtoMapper;

    @Autowired
    private ItemDtoMapper itemDtoMapper;

    @Autowired
    private ItemSImpl image;

    @Override
    public CommonResponse<List<ItemCinemaDto>> adimgUrl(Integer itemTypeId) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("itemType1Id",itemTypeId);
        wrapper.last("limit 3");
        List<ItemCinemaDto> items= itemCinemaDtoMapper.toTargets(list(wrapper));
        items.forEach(i->{
            i.setImgUrl(image.ggImageFeign(Long.valueOf(itemTypeId),2L,1L));
        });
        return ResponseUtil.returnSuccess(items);
    }

    @Override
    public CommonResponse<List<ItemDto>> getCarouselData(Integer itemTypeId) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("itemType1Id",itemTypeId);
        wrapper.last("limit 4");
        List<ItemDto> itemDto=itemDtoMapper.toTargets(list(wrapper));
        itemDto.forEach(i -> {
            i.setImgUrl(image.ggImageFeign(Long.valueOf(i.getId()),1L,1L));
        });
        return ResponseUtil.returnSuccess(itemDto);
    }

    @Override
    public CommonResponse<List<ItemCinemaDto>> getGuessYouLike(Integer itemTypeId) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("itemType1Id",itemTypeId);
        wrapper.eq("isBanner",1);
        wrapper.last("limit 6");
        List<ItemCinemaDto> cinemaDtos=itemCinemaDtoMapper.toTargets(list(wrapper));
        cinemaDtos.forEach(c->{
            c.setImgUrl(image.ggImageFeign(Long.valueOf(c.getId()),2L,1L));
        });
        return ResponseUtil.returnSuccess(cinemaDtos);
    }

    @Override
    public CommonResponse<List<ItemCinemaDto>> getWonderfulData(Integer itemTypeId) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("itemType1Id",itemTypeId);
        wrapper.eq("isRecommend",1);
        wrapper.last("limit 8");
        List<ItemCinemaDto> cinemaDtos=itemCinemaDtoMapper.toTargets(list(wrapper));
        cinemaDtos.forEach(c->{
            c.setImgUrl(image.ggImageFeign(Long.valueOf(c.getId()),2L,1L));
        });
        return ResponseUtil.returnSuccess(cinemaDtos);
    }

    @Override
    public CommonResponse<List<ItemCinemaDto>> getPopularSearch(Integer itemTypeId) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("itemType1Id",itemTypeId);
        wrapper.eq("isBanner",1);
        wrapper.last("limit 7");
        List<ItemCinemaDto> cinemaDtos=itemCinemaDtoMapper.toTargets(list(wrapper));
        cinemaDtos.forEach(c->{
            c.setImgUrl(image.ggImageFeign(Long.valueOf(c.getId()),2L,1L));
        });
        return ResponseUtil.returnSuccess(cinemaDtos);
    }

    @Override
    public CommonResponse<List<Map<String,Object>>> getCalendarData(Integer itemTypeId) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("itemType1Id",itemTypeId);
        wrapper.last("limit 31");
        List<ItemCinemaDto> cinemaDtos=itemCinemaDtoMapper.toTargets(list(wrapper));
        List list=new ArrayList();
        cinemaDtos.forEach(c->{
            Map<String,Object> map=new HashMap<>();
            c.setImgUrl(image.ggImageFeign(Long.valueOf(c.getId()),2L,1L));

            //--------------------------------------------------
            LocalDate date= LocalDate.from(c.getStartDate());
            String month=date.getMonth().getValue()+"";
            String day=date.getDayOfMonth()+"";
            if(date.getMonthValue()<10){
               month="0"+month;
            }
            if (date.getDayOfMonth()<10){
                day="0"+day;
            }
            map.put("day",Integer.valueOf(date.getYear()+""+month+""+day));
            List list1=new ArrayList();
            list1.add(c);
            map.put("itemList",list1);
            list.add(map);
        });
        return ResponseUtil.returnSuccess(list);
    }


    @Override
    public CommonResponse<List<ItemCinemaDto>> getParentChild(Integer ageGroup, Integer limit) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("ageGroup",ageGroup);
        IPage<DmItem> page=page(Page.of(0,limit),wrapper);
        List<ItemCinemaDto> itemCinemaDto=itemCinemaDtoMapper.toTargets(page.getRecords());
        itemCinemaDto.forEach(i->{
            i.setImgUrl(image.ggImageFeign(Long.valueOf(i.getId()),2L,1L));
        });
        return ResponseUtil.returnSuccess(itemCinemaDto);
    }
}
